﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXshopBuiness
{
  public static  class UserProductKJRep
    {
        public static UserProductKanJia GetById(this IRepositoryAsync<UserProductKanJia> repository, string kanJiaID)
        {
            Expression<Func<UserProductKanJia, bool>> expression = i => 1 == 1;
            if (string.IsNullOrEmpty(kanJiaID))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.KanJiaID == kanJiaID);
            }
            return repository.Query().Where(expression).Select().FirstOrDefault();
        }
        /// <summary>
        /// 通过userId productId promotionId取 （重载方法一）
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="kanJiaID"></param>
        /// <returns></returns>
        public static UserProductKanJia GetBy(this IRepositoryAsync<UserProductKanJia> repository, string userId,string productId,string promotionId,string orderID,int isused)
        {
            Expression<Func<UserProductKanJia, bool>> expression = i =>i.IsUsed == isused;
            if (string.IsNullOrEmpty(userId))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.UserID == userId);
            }
            if (string.IsNullOrEmpty(productId))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.ProductID == productId);
            }
            if (string.IsNullOrEmpty(promotionId))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.PromotionID == promotionId);
            }
            //if (string.IsNullOrEmpty(orderID))
            //{
            //    return null;
            //}
            //else
            //{
            //    expression = expression.AndAlso(i => i.OrderId == orderID);
            //}
            return repository.Query().Where(expression).Select().FirstOrDefault();
        }
        /// <summary>
        /// 通过orderId 取 （重载方法二）
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static UserProductKanJia GetBy(this IRepositoryAsync<UserProductKanJia> repository, string orderId)
        {
            Expression<Func<UserProductKanJia, bool>> expression = i => 1 == 1;
            if (string.IsNullOrEmpty(orderId))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.OrderId == orderId);
            }
            return repository.Query().Where(expression).Select().FirstOrDefault();
        }
    }
}
